$(function () {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var indentnum = $("#indentnum").val();
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/indentinfo.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#indentstatedesc").html(data.data.indentstatedesc);
                $("#username").html(data.data.name);
                $("#phone").html(data.data.phone);
                $("#address").html(data.data.address);
                $("#freight").html(data.data.freight_money);
                $("#totalmoney").html(data.data.paymoney);
                var shopinfo = data.data.indentshop;
                var type = data.data.type;
                if (type == 0)
                    $("#delivery_indent").attr("checked", true);
                else
                    $("#delivery_club").attr("checked", true);
                $(shopinfo).each(function () {
                    $("#good_info").append('<div class="goods">' +
                        '<img src="' + this.img + '" class="item_img"  onerror="imgerror(this,\'img/onerror.png\');"/>' +
                        '<ul class="item_ul">' +
                        '<li><span class="item_name">' + this.shop_name + '</span></li>' +
                        '<li>' +
                        '<div style="margin-top: 10px;">' +
                        '<span class="item_desc">' + this.name + '</span>' +
                        '<span class="item_buy_num">X' + this.num + '</span>' +
                        '</div>' +
                        '</li>' +
                        '<li><span class="item_money">￥:' + this.price + '</span></li>' +
                        '</ul>' +
                        '</div>');
                });
                var indent_state = data.data.indent_state;
                var indent_type = data.data.type;
                $("#buy").attr("onclick", "pay_indent('" + indent_type + "')");
                if (indent_type == 0) {
                    if (indent_state == 1) {
                        $("#footer").empty().append('<span class="left" onclick="indent_handle(1,\'' + indentnum + '\');">取消订单</span>' +
                            '<span class="right" onclick="indent_handle(2,\'' + indentnum + '\');">立即付款</span>');
                    } else if (indent_state == 2) {
                        $("#footer").empty().append('<span class="left" onclick="indent_handle(3,\'' + indentnum + '\');">联系客服</span>' +
                            '<span class="right" onclick="indent_handle(4,\'' + indentnum + '\');">提醒发货</span>');
                    } else if (indent_state == 3) {
                        $("#footer").empty().append('<span class="left" onclick="indent_handle(5,\'' + indentnum + '\');">查看物流</span>' +
                            '<span class="right" onclick="indent_handle(6,\'' + indentnum + '\');">确认收货</span>');
                    } else if (indent_state == 4 || indent_state == 5) {
                        $("#footer").empty().append('<span class="right" onclick="indent_handle(7,\'' + indentnum + '\');">删除</span>');
                    } else {
                        $("#footer").empty();
                    }
                } else if (indent_type == 3) {
                    if (indent_state == 1) {
                        $("#footer").empty().append('<span class="left" onclick="indent_handle(1,\'' + indentnum + '\');">取消订单</span>' +
                            '<span class="right" onclick="indent_handle(2,\'' + indentnum + '\');">立即付款</span>');
                    } else if (indent_state == 3) {
                        $("#footer").empty().append('<span class="left" onclick="indent_handle(5,\'' + indentnum + '\');">查看物流</span>');
                    }
                }
                remove_advertising();
            }
        }
    });
});

function indent_handle(state, indentnum) {
    var url = "";
    if (state == 1) {
        url = "/except/indentcontroller/cancleindent.action";
    } else if (state == 2) {
        $("#pay_div").addClass("pop_payss");
        $("#pay_bg").show();
        payindent(indentnum);
        return;
    } else if (state == 3) {
        $("#hint_message").html("开发中，敬请期待");
        $("#hintbg").show();
        return;
    } else if (state == 4) {
        url = "/except/indentcontroller/sendadvice.action";
    } else if (state == 5) {
        location.href = "/except/files/indent/logistic.jsp?indentnum="+indentnum;
        return;
    } else if (state == 6) {
        url = "/except/indentcontroller/confirmindent.action";
    } else if (state == 7) {
        url = "/except/indentcontroller/delindent.action";
    }
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                $("#hint_message").html("操作成功");
                $("#hintbg").show();
                $("#confirm_btn").hide();
                setTimeout("forword()", 3000);
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        },
        error: function (data) {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function forword() {
    var repertorytype = $("#repertorytype").val();
    if (!isempty(repertorytype)) {
        location.href = "/except/files/indent/repertory.jsp?state=" + repertorytype;
    } else {
        var indentstate = $("#state").val();
        location.href = "/except/files/indent/my_order.jsp?indentstate=" + indentstate;
    }
}

function payindent(indentnum) {
    var url = "/except/indentcontroller/indentinfo.action";
    var token = $("#token").val();
    $("#indentnum").val(indentnum);
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var sparemoney = data.data.sparemoney;
                var paymoney = data.data.paymoney;
                $("#sparemoney").html(sparemoney);
                $("#money").html(paymoney);
                if (sparemoney * 1 > paymoney * 1) {
                    $("#spare_pay").attr("checked", true);
                    $("#wechat_pay").attr("checked", false);
                } else {
                    $("#spare_pay").attr("checked", false).attr("disabled", true);
                    $("#wechat_pay").attr("checked", true);
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        },
        error: function (data) {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function showPopup() {
    $("#pay_div").removeClass("pop_payss");
    $("#pay_bg").hide();
}

function pay_indent(indent_type) {
    var indentnum = $("#indentnum").val();
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var paytype = 1;
    $("#paytype :radio").each(function () {
        var checked = $(this).attr("checked");
        if (checked)
            paytype = $(this).val();
    });
    var jsonstr = "{token:" + token + "}";
    var url = "/except/indentcontroller/payindent.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            indentnum: indentnum,
            paytype: paytype
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                if(paytype == 1) {
                    location.href = "/except/paysuccess.jsp?indenttype=" + delivery;
                } else {
                    $("#nonce_str").val(data.data.nonce_str);
                    $("#result_sign").val(data.data.result_sign);
                    $("#timestamp").val(data.data.indenttime);
                    $("#prepay_id").val(data.data.prepay_id);
                    if(indent_type == 0)
                        $("#payinfo").attr("action", "/except/files/pay.jsp");
                    else
                        $("#payinfo").attr("action", "/except/files/repertorypay.jsp");
                    $("#payinfo").submit();
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
        }
    });
}